package com.Servlet;

import com.dao.DaoFactory;
import com.entity.writer;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

public class Regist_writerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Cache-control", "no-cache");
        request.setCharacterEncoding("UTF-8");

        String nickname = request.getParameter("nickname");
        String password = request.getParameter("password");
        String email    = request.getParameter("email");
        String sex      = request.getParameter("sex");
        String truename = request.getParameter("truename");
        String phone    = request.getParameter("phone");
        String idcard   = request.getParameter("idcard");

        System.out.println("---------------作者注册--------------");
        System.out.println("用户名:" + nickname);
        System.out.println("密码:" + password);
        System.out.println("邮箱:" + email);
        System.out.println("sex:" + sex);
        System.out.println("真实姓名:" + truename);
        System.out.println("手机号:" + phone);
        System.out.println("身份证号:" + idcard);
        System.out.println("------------------------------------");


        HttpSession session  = request.getSession();

        writer userman = new writer();
        userman.setNickname(nickname);
        userman.setPassword(password);
        userman.setEmail(email);
        userman.setSex(sex);
        userman.setPhone(phone);
        userman.setIdcard(idcard);
        userman.setTruename(truename);

        try {

            writer check_phone = DaoFactory.getInstance().getwriterDao().findByPhone(phone);
            writer check_email = DaoFactory.getInstance().getwriterDao().findByEmail(email);
            writer check_nickname = DaoFactory.getInstance().getwriterDao().findBynickname(nickname);


            if(check_phone == null && check_email == null && check_nickname == null){
                //执行跳转
                DaoFactory.getInstance().getwriterDao().add(userman);
                //执行跳转
                System.out.println("注册成功");
//                session.setAttribute("user", entity);
                request.getRequestDispatcher("Login.jsp").forward(request,response);
            }
            else if(check_email != null){
                //返回给用户“错误”
                System.out.println("错误，邮箱已存在");
                response.sendRedirect("regist_writer.jsp");

            }
            else if(check_phone != null){
                //返回给用户“错误”
                System.out.println("错误，手机号已被注册");
                response.sendRedirect("regist_writer.jsp");
            }
            else if(check_nickname != null){
                //返回给用户“错误”
                System.out.println("错误，用户名已存在");
                response.sendRedirect("regist_writer.jsp");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
